<?php

/**
 * 新增首页美食联盟推荐
 * 
 * @copyright xinwei
 * @since 2012-04-09
 */
/*ini_set( "display_errors",true);
error_reporting(E_ALL); */
include_once($_SERVER["DOCUMENT_ROOT"] . "/core/inc.php");
include_once($_SERVER["DOCUMENT_ROOT"] . "/core/admin.inc.php");
include_once($_SERVER["DOCUMENT_ROOT"] . "/include/foodv2/inc.php");

$g_cgival = array();
$g_pro = array();
$g_show = array();


function check_cgi_pro()
{
	global $g_cgival, $g_pro, $g_show;
	
	if(! security_check::check_refer())
	{
		throw new Exception("来路不明");
	}
	
	//推荐ID
	cgi::both($g_cgival["tj_id"], "id", 0);
	$g_cgival["tj_id"] = intval($g_cgival["tj_id"]);
	
	//餐厅名称
	cgi::both($g_cgival["rstrt_name"], "rstrt_name", "");
	$g_cgival["rstrt_name"] = string::un_script_code($g_cgival["rstrt_name"]);
	$g_cgival["rstrt_name"] = string::un_html($g_cgival["rstrt_name"]);
	
	//餐厅图片
	cgi::both($g_cgival["rstrt_image"], "rstrt_image", "");
	$g_cgival["rstrt_image"] = string::un_script_code($g_cgival["rstrt_image"]);
	$g_cgival["rstrt_image"] = string::un_html($g_cgival["rstrt_image"]);
	
	//餐厅链接
	cgi::both($g_cgival["rstrt_url"], "rstrt_url", "");
	$g_cgival["rstrt_url"] = string::un_script_code($g_cgival["rstrt_url"]);
	$g_cgival["rstrt_url"] = string::un_html($g_cgival["rstrt_url"]);
}

function get_data()
{
	global $g_cgival, $g_pro, $g_show;

	$admin_log = new admin_log();
	$db = new db_food_alliance_recommend();
	
	// 拿到当前站点的site_id
	$site_id = admin::admin_get_user_site_id();
	$admin_id = admin::admin_get_login_uid();	// 拿到当前管理员ID
	$admin_name = admin::admin_get_login_user_name();	// 拿到当前管理员用户名

	if ($_FILES['file1'])
	{

		try
		{
			/* 上传图片 */
			$upload = new s3_upload();
			$filename = "/cove_template/".time();
			$g_show['data'] = $upload->show($_FILES['file1'])->zomm(50,50)->save($filename);
			$g_show['data'] = $g_show['data']['src'];
			
		}
		catch (Exception $e)
		{
			throw new Exception($e->getMessage(), 1);
			
		}
	}
	else
	{
		
		if($g_cgival["tj_id"] <=0)
		{
			//数量判断
			$num_check = $db->admin_get_food_alliance_recommend_count_by_site_id($site_id,$g_cgival["template_name"],false,'',1,1);
			if($num_check>=30)
			{
				throw new Exception("推荐数量已达到30个，无法继续推荐。");
			}
		}
		
		
		//url的http://处理
		$g_cgival["rstrt_url"] = substr($g_cgival["rstrt_url"],0,5)=="http:"?$g_cgival["rstrt_url"]:"http://".$g_cgival["rstrt_url"];
		
		
		//判断新增/更新
		if($g_cgival["tj_id"]>0)
		{
			//更新
			$data = $db->admin_insert_food_alliance_recommend_by_id($g_cgival["tj_id"],$g_cgival["rstrt_name"],$g_cgival["rstrt_image"],$g_cgival["rstrt_url"],$admin_id,$admin_name);
			$admin_log->admin_insert_log_info($admin_id, $site_id, 16, "编辑 首页美食联盟id={$g_cgival["tj_id"]}");
		}
		else
		{
			//新增
			$data = $db->admin_insert_food_alliance_recommend($site_id,$g_cgival["rstrt_name"],$g_cgival["rstrt_image"],$g_cgival["rstrt_url"],$admin_id,$admin_name);
			$admin_log->admin_insert_log_info($admin_id, $site_id, 16, "新增 首页美食联盟");
		}
		
		if(!$data)
		{
			throw new Exception("操作失败");
		}
	}
	
	
}

$g_show["error"] = 0;
$g_show["errmsg"] = '';
try
{
	admin_check_login();
// 	admin_check_user_priv("foodv2/album_cover_template_add_post.php");
	
	check_cgi_pro();
	get_data();
}
catch(Exception $e)
{
	$g_show["error"] = 1;
	$g_show["errmsg"] = $e->getMessage();
}

show_pro();
exit;


function show_pro()
{
	global $g_cgival, $g_pro, $g_show;
	if(isset($_GET['debug']))
		print_r($g_show);
	//echo output::json('json',$g_show);
	echo json_encode($g_show);
}

?>